package me.ivps.android.cropdemo;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
    
    private static final int RequestCode_TakePhoto = 1;
    private static final int RequestCode_Gallery = 2;
    private static final int RequestCode_Cut = 3;
    
    private View mBtnCamera;
    private TextView mText;
    private ImageView mImage;
    
    private File mTmpFile = new File(Environment.getExternalStorageDirectory(),
            getPhotoFileName());
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
        mBtnCamera = findViewById(R.id.btn_camera);
        mBtnCamera.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
                startActivityForResult(intent, RequestCode_TakePhoto);
            }
        });
        
        mText = (TextView) findViewById(R.id.text);
        mImage = (ImageView) findViewById(R.id.image);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RequestCode_TakePhoto:
                startPhotoZoom(Uri.fromFile(mTmpFile));
                break;
            case RequestCode_Gallery:
                if (data != null) {
                    startPhotoZoom(data.getData());
                }
                break;
            case RequestCode_Cut:// 返回的结果
                if (data != null) {
                    sentPicToNext(data);
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }
    
    /**
     * 启动裁切
     * 
     * @param uri
     */
    private void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        
        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, RequestCode_Cut);
    }
    
    private void sentPicToNext(Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Bitmap photo = bundle.getParcelable("data");
            if (photo != null) {
                mImage.setImageBitmap(photo);
                mText.setText("已裁切, " + photo.getWidth() + ", "
                        + photo.getHeight());
            }
        }
    }
    
    private String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "'IMG'_yyyyMMdd_HHmmss", Locale.US);
        return dateFormat.format(date) + ".jpg";
    }
    
}
